<template>
  <div id="userManagePage">
    <a-form layout="inline" :model="seachrParams" @finish="doSearch">

      <a-form-item label="账号">
        <a-input v-model:value="seachrParams.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item label="用户名">
        <a-input v-model:value="seachrParams.userName" placeholder="请输入用户名" />
      </a-form-item>
      <a-form-item >
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
<!--    表格-->
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'userAvatar'">
          <a-image :src="record.userAvatar" width="120" />
        </template>
        <template v-else-if="column.dataIndex === 'userRole'">
          <div v-if="record.userRole === 'admin'">
            <a-tag color="green">管理员</a-tag>
          </div>
          <div v-else>
            <a-tag color="blue">普通用户</a-tag>
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createdTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>

        <template v-else-if="column.key === 'action'">
          <a-button danger @click="doDelete(record.id)">删除</a-button>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>

import {  computed, onMounted, reactive, ref } from 'vue'
import { deleteUserUsingPost, listUserVoByPageUsingPost } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
const columns = [
  {
    title: 'id',
    dataIndex: 'id',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
  },
  {
    title: '用户名',
    dataIndex: 'userName',
  },
  {
    title: '头像',
    dataIndex: 'userAvatar',
  },
  {
    title: '简介',
    dataIndex: 'userProfile',
  },
  {
    title: '角色',
    dataIndex: 'userRole',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },

  {
    title: '操作',
    key: 'action',
  },
]
// 定义数据 ref 可以接受原始类型（如布尔值、字符串、数字）或对象作为参数
// ，而 reactive 只能接受对象。对于对象，两者都可以使用，但 ref 会通过 .value 属性访问内容，而 reactive 则可以直接访问。
const dataList = ref<API.UserVO[]>([])
const total = ref(0)
// 搜索条件
const seachrParams = reactive<API.UserQueryRequest>({
  current: 1,
  pageSize: 10,
  userAccount: '',
  userName: '',
})


// 分页参数  computed 计算函数  seachrParams 改变的时候可以跟着动态改变
const pagination = computed(() => {
  return {
    current: seachrParams.current,
    pageSize: seachrParams.pageSize,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total) => `合计${total}条`,
  }
})

// 表格发生变化
const doTableChange = (page: any) => {
  seachrParams.current = page.current
  seachrParams.pageSize = page.pageSize
  fetchData()
}

// 获取数据   ...seachrParams 数据展开
const fetchData = async () => {
  const res = await listUserVoByPageUsingPost({
    ...seachrParams,
  })
  if (res.data.code == 0 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败' + res.data.message)
  }
}
const  doSearch = () => {
  seachrParams.current = 1
  fetchData()
}
// 删除
const doDelete = async (id:string) => {
  if (!id){
    message.error('id不存在')
    return
  }
  const res = await deleteUserUsingPost({ id })
  if (res.data.code === 0) {
    message.success('删除成功')
    // 刷新数据
    fetchData()
  }else {
    message.error('删除失败')
  }
}
// 页面加载时候调用数据
onMounted(() => {
  fetchData()
})
</script>
